﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Interop;

namespace ManhattanProcedure.Model
{
    public class RobotOperate
    {
        public static string RobotOperateUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        public static int handle1 = 0;
        public static int handle2 = 0;
        public static int handle3 = 0;
        public static double PI = 3.1415926535;
        public static List<string[]> listStrArr1 = new List<string[]>();//数组List，相当于可以无限扩大的二维数组。
        public static List<string[]> listStrArr2 = new List<string[]>();
        public static List<string[]> listStrArr3 = new List<string[]>();//数组List，相当于可以无限扩大的二维数组。
        public static List<string[]> listStrArr4 = new List<string[]>();
        public static JKTYPE.ProgramState pstatusR;
        public static JKTYPE.ProgramState pstatusL;
        public static JKTYPE.RobotState stateL;
        public static JKTYPE.RobotStatus statusL;
        public static JKTYPE.RobotState stateR;
        public static JKTYPE.RobotStatus statusR;
        private static RobotOperate instance;
        private RobotOperate() { }
        public static RobotOperate Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RobotOperate();
                }
                return instance;
            }
        }
        public static void Read_CSV()//读取轨迹文件
        {
            string line1 = "";
            string line2 = "";
            string line3 = "";
            string line4 = "";
            string line5 = "";
            string line6 = "";
            string path1 = RobotOperateUrl + "\\cup_swan_single1.csv";
            StreamReader reader1 = new StreamReader(path1);

            line1 = reader1.ReadLine();
            while (line1 != null)
            {
                listStrArr1.Add(line1.Split(','));//将文件内容分割成数组
                line1 = reader1.ReadLine();
            }
            reader1.Close();

            StreamReader reader2 = new StreamReader(RobotOperateUrl + "/milk_swan_single_adj4.csv");
            line2 = reader2.ReadLine();
            while (line2 != null)
            {
                listStrArr2.Add(line2.Split(','));//将文件内容分割成数组
                line2 = reader2.ReadLine();
            }
            reader2.Close();
            StreamReader reader3 = new StreamReader(RobotOperateUrl + "/cup_malaysia_tulip.csv");
            line3 = reader3.ReadLine();
            while (line3 != null)
            {
                listStrArr3.Add(line3.Split(','));//将文件内容分割成数组
                line3 = reader3.ReadLine();
            }
            reader3.Close();

            StreamReader reader4 = new StreamReader(RobotOperateUrl + "/milk_malaysia_tulip.csv");
            line4 = reader4.ReadLine();
            while (line4 != null)
            {
                listStrArr4.Add(line4.Split(','));//将文件内容分割成数组
                line4 = reader4.ReadLine();
            }
            reader4.Close();
        }
        public void Robot_Initialisation_double()//机器人初始化
        {
            IOMsg.Instance.AllBol = false;
            Console.WriteLine(DigitalMsg.Instance.RobotPowerOn_start);
            Thread.Sleep(200);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn_start);
            Thread.Sleep(3000);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_start);
            Thread.Sleep(500);
            int tool_idset1 = 0;
            int frame_idset1 = 0;
            int tool_idset2 = 0;
            int frame_idset2 = 0;
            int result1 = -1;
            int result2 = -1;
            while (result1 == -1)
            {
                //Thread.Sleep(200);
                //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn_start);
                //Thread.Sleep(500);
                result1 = jakaAPI.create_handler(IPorPortInfo.Instance.RobotIPLeft.ToCharArray(), ref handle1);
                Thread.Sleep(100);
            }
            while (result2 == -1)
            {
                //Thread.Sleep(1500);
                //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_start);
                //Thread.Sleep(500);
                result2 = jakaAPI.create_handler(IPorPortInfo.Instance.RobotIPRight.ToCharArray(), ref handle2);
                Thread.Sleep(100);
            }
            IOMsg.Instance.AllBol = false;
            Thread.Sleep(100);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn_end);
            Thread.Sleep(100);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_end);
            Thread.Sleep(100);
            IOMsg.Instance.AllBol = true;
            System.Threading.Thread.Sleep(3000); //毫秒
            //机器人上电
            jakaAPI.power_on(ref handle1); Console.WriteLine("L_Robot Power On");
            //机器人上电
            jakaAPI.power_on(ref handle2); Console.WriteLine("R_Robot Power On");
            //切换当前使用的工具坐标
            jakaAPI.set_tool_id(ref handle1, tool_idset1); Console.WriteLine("L_Robot_T " + tool_idset1);
            //切换当前使用的世界坐标
            jakaAPI.set_user_frame_id(ref handle1, frame_idset1); Console.WriteLine("L_Robot_F " + frame_idset1);

            //切换当前使用的工具坐标
            jakaAPI.set_tool_id(ref handle2, tool_idset2); Console.WriteLine("R_Robot_T " + tool_idset2);
            //切换当前使用的世界坐标
            jakaAPI.set_user_frame_id(ref handle2, frame_idset2); Console.WriteLine("R_Robot_F " + frame_idset2);
            StringBuilder Version = new StringBuilder("", 3000);
            int result3 = jakaAPI.get_sdk_version(ref handle1, Version);
            Console.WriteLine("SDK 版本 " + Version);
            IOMsg.Instance.AllBol = false;
            Thread.Sleep(100);
            IOMsg.Instance.QK();
            Thread.Sleep(100);
            IOMsg.Instance.AllBol = true;
            ClientList clientList2 = new ClientList();
            clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
            clientList2.code = 12;
            SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
            Thread thr2 = new Thread(MainWindow.MessageShow);
            thr2.IsBackground = true;
            thr2.Start();
        }
    }
}
